<?
#include("DataBase.class.php");
#include("../../classes/business/socios/Socio.class.php");
#include("ORM.class.php");

class SQLHelper
{
var $name_array;
var $int_array;
var $index;
var $sql;
var $hasWhere;

	function SQLHelper()
	{
	$this->name_array = array();
	$this->int_array = array();
	$this->select_array = array();
	$this->index = -1;
	$this->hasWhere = FALSE;
	}
	
	function AddItem($field_name,$originalValue, $sqlOperator = "")
	{
		$value = addslashes($originalValue);
		$this->name_array[$field_name] = $value;
		$this->index++;
		$this->int_array[$this->index] = $value;
		$this->select_array[$field_name]["value"] = $value;
		$this->select_array[$field_name]["sqlOperator"] = $sqlOperator;
	}
	
	function Clear()
	{
		$this->SQLHelper();
	}
	
	function GetItem($x)//este metodo acepta int o string
	{
		if(is_int($x))
			return $this->int_array[$x];
		if(is_string($x))
			return $this->name_array[$x];
	}
	
	function InsertStatement($table, $has_sqlFunctions = "")
	{
		$tmp = -1;
		$this->sql = "INSERT into $table SET\n";

		foreach( $this->name_array as $key => $value ){
			if(strpos($value,"(") && $has_sqlFunctions)
			   $this->sql.= "$key = $value,\n";
			else
			   $this->sql.= "$key = '$value',\n";
		}

		 return substr($this->sql,0,strlen($this->sql)-2);
	
	}
	
	function UpdateStatement($table,$id, $campo = "id", $otro_campo="",$otro="")
	{
		$tmp = -1;
		$this->sql = "UPDATE $table SET\n";
		
		foreach( $this->name_array as $key => $value ) 
		{
			$tmp++;
			if(is_string($value) && $tmp < $this->index)
			   $this->sql.= "$key = '$value',\n";
			if(is_int($value) && $tmp < $this->index)
			   $this->sql.= "$key = $value,\n";
			if(is_string($value) && $tmp == $this->index)
			   $this->sql.= "$key = '$value'\n";
			if(is_int($value) && $tmp == $this->index)
			   $this->sql.= "$key = $value\n";
		}
		if(is_string($id))
			$this->sql.= "WHERE $campo = '$id'";
		if(is_int($id))
			$this->sql.= "WHERE $campo = $id";
		if($otro_campo!="")
		$this->sql.=" AND $otro_campo = \"$otro\"";

		 return $this->sql;
	}
	
	function sql_select($table)
	{
		$tmp = -1;
		$this->sql = "SELECT * FROM $table \n";
		foreach( $this->select_array as $key => $value ) 
		{
			if($tmp == -1)
				$this->sql .= "WHERE \n";
			$tmp++;
			if($tmp < $this->index)
				if($value[sqlOperator] == "LIKE")
					$this->sql.= "$key $value[sqlOperator] '%$value[value]%' AND\n";
				else
					$this->sql.= "$key $value[sqlOperator] '$value[value]' AND\n";
									
			if($tmp == $this->index)
				if($value[sqlOperator] == "LIKE")
					$this->sql.= "$key $value[sqlOperator] '%$value[value]%'\n";
				else
					$this->sql.= "$key $value[sqlOperator] '$value[value]'\n";
		}
		
		if($tmp > -1){
			$this->hasWhere = TRUE;
		}

		 return $this->sql;
	}
	
	
}#end Class SQLHelper
#SQLHelper::SQLStatement();
?>